package com.yupi.project.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class TopicProducer {

  private static final String EXCHANGE_NAME = "topic_logs";

  public static void main(String[] argv) throws Exception {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      try (Connection connection = factory.newConnection();
           Channel channel = connection.createChannel();
           Scanner scanner = new Scanner(System.in)) {
          channel.exchangeDeclare(EXCHANGE_NAME, "topic");
          while (scanner.hasNextLine()) {
              String input = scanner.nextLine();
              String[] split = input.split(" ");
              String message = split[0];
              String rountine = split[1];
              channel.basicPublish(EXCHANGE_NAME, rountine, null, message.getBytes("UTF-8"));
              System.out.println(" [x] Sent '" + rountine + "':'" + message + "'");
          }
      }
  }
  //..
}